package com.sdy.healthcode.web.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.DesensitizeUtil;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.dto.GetAppointmentDTO;
import com.sdy.healthcode.biz.model.HsDepartment;
import com.sdy.healthcode.biz.param.DelDeptByIdParam;
import com.sdy.healthcode.biz.param.UpdateDeptOrAddDeptParam;
import com.sdy.healthcode.biz.service.ExpertsAppointmentService;
import com.sdy.healthcode.biz.service.HsDepartmentService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 预约情况
 * </p>
 *
 * @author hyh
 * @since 2020-03-09
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@Api(tags = "预约情况Api")
@RequestMapping("/appointment")
public class AppointmentController extends BaseController {
    @Autowired
    private ExpertsAppointmentService appointmentService;

    @ApiOperation(value = "获取预约挂号信息", response = GetAppointmentDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "预约状态 1-有效 2-已取消 3-已过期"),
            @ApiImplicitParam(name = "docState", value = "医生状态 1、可预约 2、长期停诊 3、停诊不显示 4、临时停诊"),
            @ApiImplicitParam(name = "name", value = "根据患者名称查询"),
            @ApiImplicitParam(name = "startTime", value = "起始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")
    })
    @GetMapping("/getAppointmentPageList")
    public Response getAppointmentPageList(Integer state, Integer docState, String name, Date startTime, Date endTime, Page page) throws BizException, UnsupportedEncodingException {
        if (null != name) {
            name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String startTimeStr = null;
        String endTimeStr = null;
        if (null != startTime) {
            startTimeStr = format.format(startTime);
            endTimeStr = format.format(endTime);
        }
        IPage<GetAppointmentDTO> pageList = appointmentService.getAppointmentPageList(state, docState, name, startTimeStr, endTimeStr, page);
        // 隐藏身份证号
        if (pageList.getRecords() != null) {
            for (GetAppointmentDTO record : pageList.getRecords()) {
                record.setIdCard(DesensitizeUtil.hideIdCardNo(record.getIdCard()));
            }
        }
        return Response.success(pageList);
    }

    /**
     * 导出预约信息记录(Excel文件)
     */
    @ApiOperation(value = "导出预约信息记录(Excel文件)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "预约状态 1-有效 2-已取消 3-已过期"),
            @ApiImplicitParam(name = "docState", value = "医生状态 1、可预约 2、长期停诊 3、停诊不显示 4、临时停诊"),
            @ApiImplicitParam(name = "name", value = "根据患者名称查询"),
            @ApiImplicitParam(name = "startTime", value = "起始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")
    })
    @GetMapping("/downLoadDeptListToExcel")
    public void downLoadDeptListToExcel(Integer state, Integer docState, String name, Date startTime, Date endTime, HttpServletResponse response) throws IOException, BizException {
        if (null != name) {
            name = URLDecoder.decode(name, StandardCharsets.UTF_8.name());
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String startTimeStr = null;
        String endTimeStr = null;
        if (null != startTime) {
            startTimeStr = format.format(startTime);
            endTimeStr = format.format(endTime);
        }
        List<GetAppointmentDTO> list = appointmentService.getAppointmentList(state, docState, name, startTimeStr, endTimeStr);
        // 隐藏身份证号
        for (GetAppointmentDTO record : list) {
            record.setIdCard(DesensitizeUtil.hideIdCardNo(record.getIdCard()));
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sdf.format(new Date());
        String fileName = date + "预约信息记录.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        EasyExcel.write(response.getOutputStream(), GetAppointmentDTO.class).sheet("预约信息记录").doWrite(list);
    }

}
